package com.tigercel.xiangchengyun.ui.activities

import android.content.Intent
import android.os.Bundle
import android.support.annotation.DrawableRes
import android.support.v4.view.PagerAdapter
import android.support.v4.view.ViewPager
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import com.tigercel.xiangchengyun.R
import kotlinx.android.synthetic.main.activity_guide.*
import java.util.*

class GuideActivity : AppCompatActivity() {
    private val imageViewList = ArrayList<ImageView>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_guide)
        initImageView(R.mipmap.slide_one)
        initImageView(R.mipmap.slide_two)
        initImageView(R.mipmap.slide_three)
        viewPager.adapter = GuideViewPagerAdapter(imageViewList)
        viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

            }

            override fun onPageSelected(position: Int) {
                changeViewPagerState(position)
            }

            override fun onPageScrollStateChanged(state: Int) {

            }
        })
        changeViewPagerState(0)
        button.setOnClickListener({ gotoMainActivity() })
    }

    private fun gotoMainActivity() {
        val intent = Intent(this, MainActivity::class.java)
        startActivity(intent)
        finish()
    }

    private fun initImageView(@DrawableRes drawRes: Int) {
        val imageView = ImageView(this)
        val lp = ViewPager.LayoutParams()
        lp.height = ViewPager.LayoutParams.MATCH_PARENT
        lp.width = ViewPager.LayoutParams.MATCH_PARENT
        imageView.scaleType = ImageView.ScaleType.FIT_XY
        imageView.layoutParams = lp
        imageView.setImageResource(drawRes)
        imageViewList.add(imageView)
    }

    /**
     * 修改和ViewPager当前页面position相关状态
     * @param position
     */
    fun changeViewPagerState(position: Int) {
        if (position == imageViewList.size - 1) {
            button.visibility = View.VISIBLE
        } else {
            button.visibility = View.GONE
        }
    }

    inner class GuideViewPagerAdapter (imageViewList: List<ImageView>) : PagerAdapter() {

        private var imageViewList = ArrayList<ImageView>()

        init {
            this.imageViewList = imageViewList as ArrayList<ImageView>
        }

        override fun getCount(): Int {
            return this.imageViewList?.size
        }

        override fun isViewFromObject(view: View, `object`: Any): Boolean {
            return view === `object`
        }

        override fun instantiateItem(container: ViewGroup, position: Int): Any {
            container.addView(this.imageViewList[position])
            return this.imageViewList[position]
        }

        override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
            container.removeView(this.imageViewList[position])
        }
    }
}
